// pages/member/member.js
var request = require("../../utils/request.js")
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startX: 0,
    endX: 0,
    touchShow: false,
    showModal: false,
    userList:[],
    delIndex:"",
    info:"",
    info1:"",
    s:"0",
    m:'0'
  },
  //手指触摸动作开始
  touchstart: function (e) {
    console.log(e)
    this.data.userList.forEach(function(v,i){
      //console.log(v);
      v.touchShow = false;
    })
    var startX = e.changedTouches[0].clientX;
    this.setData({
      startX: startX,
      userList:this.data.userList
    })
  },
  // touchE: function (e) {
  //   // console.log(e);
  //   var that = this
  //   if (e.changedTouches.length == 1) {
  //   //手指移动结束后触摸点位置的X坐标
  //   var endX = e.changedTouches[0].clientX;
  //   //触摸开始与结束，手指移动的距离
  //   var disX = that.data.startX - endX;
  //   var delBtnWidth = 80;
  //   //如果距离小于删除按钮的1/2，不显示删除按钮
  //   var txtStyle = disX > delBtnWidth / 2 ? true : false;
  //   console.log(txtStyle);
  //   //获取手指触摸的是哪一项
  //   var index = e.currentTarget.dataset.index;
  //   var list = that.data.userList;
  //   list[index].touchShow = txtStyle;
  //   //更新列表的状态
  //   that.setData({
  //   userList: list
  //   });
  //   }
  //   },
  touchmove: function (e) {
    //console.log(e);
    var that = this;
    var index = e.currentTarget.dataset.index;//当前索引
    var startX = that.data.startX;//开始X坐标
    var startY = that.data.startY; //开始Y坐标
    var touchMoveX = e.changedTouches[0].clientX; //滑动变化坐标
    let item = e.currentTarget.dataset.item;
    var endX = e.changedTouches[0].clientX;
    var touchMoveY = e.changedTouches[0].clientY; //滑动变化坐标
   

    this.data.userList.forEach(function(v,i){
      v.touchShow = false;


      if(i == index){
        if(touchMoveX>startX){
          v.touchShow = false;
        }else{
          if((startX - touchMoveX)>80){
            v.touchShow = true;
          }
          
        }
      }
      
    })
    this.setData({
      userList:this.data.userList,
      endX:endX
    })
    //console.log(this.data);
    // if (Math.abs(parseInt(endX) - parseInt(this.data.startX)) > 80) {
    //   this.setData({
    //     touchShow: parseInt(endX) - parseInt(this.data.startX) < 0 ? true : false,
    //     endX: endX
    //   })
    // }
  },

  //删除
  del: function (e) {
    console.log(e);
    this.setData({
      showModal: true,
      delIndex:e.currentTarget.dataset.id
    })
  },
  //确认删除
  modalConfirm:function(){
    //console.log(111)
    var that = this
    that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框

    request.postData('xcx/deleteMyUser', {uid:that.data.delIndex}, function (res) {
      // success
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == '0') {
          console.log(res)
          that.getUserList()
          if(wx.getStorageSync("uid")!=undefined && wx.getStorageSync("uid")!=''){
            wx.setStorage({
              key: 'uid',
              data: ''
            })
          }
      } else {
          that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
      }

  }, function (res) {
      // error
      that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
  });
  },
  //编辑
  edit: function (a) {
    var e = a.currentTarget.dataset.item;
    console.log(e);
    wx.navigateTo({
      url: '../EditMember/EditMember?name='+e.name+'&birth='+e.birth+'&address='+e.address+'&mobile='+e.mobile+'&jiancha='+e.jiancha+'&province='+e.province+'&city='+e.city+"&dateType=update"+"&id="+a.currentTarget.dataset.id+"&photo="+e.photo,
    })
  },
  //添加成员
  addbtn:function(){
    wx.navigateTo({
      url: '../EditMember/EditMember?dateType=create',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.toastDialog = this.selectComponent("#toastDialog");//此处id和页面的 wxml中的id一致

    wx.showShareMenu({ menus: ['shareAppMessage', 'shareTimeline']})
  },
/**
 * 用户点击右上角分享
 */
onShareAppMessage (res) {
  return {
    title: '迪佳极视',
    path: 'pages/index/index',
    // imageUrl:'这个是显示的图片，不写就默认当前页面的截图',
    success: function (shareTickets) {
      console.info(shareTickets + '成功');
      // 转发成功
    },
    fail: function (res) {
      console.log(res + '失败');
      // 转发失败
    },
    complete:function(res){
      // 不管成功失败都会执行
    }
  }
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.getUserList()
  },
  getUserList:function(){
    var that = this
    that.toastDialog.showDialog({ code: 'loading', message: '加载中', timeout: false }); //loading 加载框

    request.postData('xcx/myUserList', {}, function (res) {
      // success
      that.toastDialog.hideDialog(); //关闭loading提示框
      if (res.code == '0') {
          console.log(res)
          if(res.userList.length != 0){
            for(var i = 0; i<res.userList.length;i++){
              res.userList[i].touchShow = false
            }
            that.setData({
              userList:res.userList
            })
          }else{
            that.setData({
              info:"暂无数据~",
              s:"1",
              m:'1'
            })
          }
         
      } else {
          that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
      }

  }, function (res) {
      // error
      that.toastDialog.showDialog({ code: res.code, message: res.message, timeout: true });
  });
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("member-show")
    this.getUserList()

  },
})